Typical HAL TIM use cases for migration ¶
This section provides information about typical HAL TIM use cases for migration.
Specific TIM Concept ¶
|
Functionality |
HAL1 |
HAL2 |
|---|---|---|
|
Initialization |
|
|
|
Functionality |
HAL1 |
HAL2 |
|---|---|---|
|
Deinitialization |
|
|
|
Functionality |
HAL1 |
HAL2 |
|---|---|---|
|
Start and Stop |
|
|
|
Functionality |
HAL1 |
HAL2 |
|---|---|---|
|
Channel Configuration |
|
|
TIM use cases ¶
Use case: Time base in interrupt mode ¶
|
HAL1 |
HAL2 |
|---|---|
|
Start TIMx in interrupt mode
TIMx IRQ handler
Callback function
|
Start TIMx in interrupt mode
TIMx IRQ handler
Callback function
|
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
/* Declaration of the tim handle */
TIM_HandleTypeDef htim2;
/* Ensure that all required peripherals
(TIM, GPIO, NVIC, etc.) are configured properly
before starting the timer.*/
.............
/* Start the timer TIMx in interrupt mode */
HAL_TIM_Base_Start_IT(&htim2);
/* User must call the Interrupt Handler
functions in TIMx global interrupt */
void TIM2_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim2);
}
/* Call the callback function */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* Code to execute at each interrupt
For example, toggle an LED */
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
|
/* Declaration of the tim handle */
hal_tim_handle_t htim2;
/* Ensure that all required peripherals
(TIM, GPIO, NVIC, etc.) are configured properly
before starting the timer.*/
.............
/* Start the timer TIMx in interrupt mode */
HAL_TIM_Start_IT(&htim2);
/* User must call the Interrupt Handler
functions in TIM2 global interrupt */
void TIM2_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim2);
}
/* Call the callback function */
void HAL_TIM_UpdateCallback(hal_tim_handle_t *htim)
{
/* Code to execute at each interrupt
For example, toggle an LED */
HAL_GPIO_TogglePin(HAL_GPIOA, HAL_GPIO_PIN_5);
}
|
Key changes ¶
The time base start API is renamed from
HAL_TIM_Base_Start_IT()toHAL_TIM_Start_IT().The update callback is renamed from
HAL_TIM_PeriodElapsedCallback()toHAL_TIM_UpdateCallback().HAL2 provides a dedicated update IRQ handler
HAL_TIM_UPD_IRQHandler()in addition to the global handlerHAL_TIM_IRQHandler().
Use case: PWM output in interrupt mode ¶
|
HAL1 |
HAL2 |
|---|---|
|
Channel configuration
Start PWM signal in interrupt mode
TIMx IRQ handler
Callback function
|
Channel configuration
Start PWM signal in interrupt mode
TIMx IRQ handler
Callback function
|
Important
When using PWM with DMA, it is mandatory to call the function
HAL_TIM_OC_EnableChannelComparePreload.
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
/* Declaration of tim handle */
TIM_HandleTypeDef htim3;
/* Ensure that all required peripherals
(TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
before starting PWM output.*/
.............
/* Start the PWM output in interrupt mode */
HAL_TIM_PWM_Start_IT(&htim3, TIM_CHANNEL_2);
/* User must call the Interrupt Handler
functions in TIM3 global interrupt */
void TIM3_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim3);
}
/* Call the callback function */
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
/* Code to execute at each compare match interrupt
For example, toggle an LED */
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
|
/* Declaration of the tim handle */
hal_tim_handle_t htim3;
/* Ensure that all required peripherals
(TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
before starting PWM output.*/
.............
/* Start a timer's output channel in interrupt mode. */
HAL_TIM_OC_StartChannel_IT(&htim3, HAL_TIM_CHANNEL_2);
/* Start the timer */
HAL_TIM_Start(&htim3);
/* User must call the Interrupt Handler
functions in TIM2 global interrupt */
void TIM2_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim3);
}
/* Call the callback function */
void HAL_TIM_CompareMatchCallback(hal_tim_handle_t *htim,
hal_tim_channel_t channel);
{
/* Code to execute at each interrupt
For example, toggle an LED */
HAL_GPIO_TogglePin(HAL_GPIOA, HAL_GPIO_PIN_5);
}
|
Key changes ¶
PWM configuration APIs are replaced by the output compare configuration sequence in HAL2.
Channel start in interrupt mode uses
HAL_TIM_OC_StartChannel_IT()and timer start usesHAL_TIM_Start().The PWM pulse finished callback is replaced by the output compare match callback
HAL_TIM_CompareMatchCallback().
Related examples in HAL2 ¶
For more details, refer to the complete example under the firmware directory.
|
Example name |
Brief |
|---|---|
|
pwm_output |
How to configure the TIM peripheral in PWM (Pulse Width Modulation) mode. The PWM waveforms generated by the timer channels can be displayed using an oscilloscope. |
Use case: Input capture in interrupt mode ¶
|
HAL1 |
HAL2 |
|---|---|
|
Start input capture in interrupt mode
TIMx IRQ handler
Callback function
|
Start input capture in interrupt mode
TIMx IRQ handler
Callback function
|
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
/* Declaration of tim handle */
TIM_HandleTypeDef htim3;
/* Ensure that all required peripherals
(TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
before starting the input capture.*/
.............
/* Start the Input Capture in interrupt mode */
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);
/* User must call the Interrupt Handler
functions in TIM3 global interrupt */
void TIM3_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim3);
}
/* Call the callback function */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
/* Get the Input Capture value */
uwIC2Value = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_2);
}
|
/* Declaration of the tim handle */
hal_tim_handle_t htim3;
/* Ensure that all required peripherals
(TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
before starting the input capture.*/
.............
/* Start a timer's output channel in interrupt mode. */
HAL_TIM_IC_StartChannel_IT(&htim3, HAL_TIM_CHANNEL_2);
/* Start the timer */
HAL_TIM_Start(&htim3);
/* User must call the Interrupt Handler
functions in TIM3 global interrupt */
void TIM3_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim3);
}
/* Call the callback function */
void HAL_TIM_InputCaptureCallback(hal_tim_handle_t *htim,
hal_tim_channel_t channel);
{
/* Get the Input Capture value */
uwIC2Value = HAL_TIM_IC_ReadChannelCapturedValue(&htim3, HAL_TIM_CHANNEL_2)
}
|
Key changes ¶
Input capture start API changes from
HAL_TIM_IC_Start_IT()toHAL_TIM_IC_StartChannel_IT()and requiresHAL_TIM_Start().The callback is renamed from
HAL_TIM_IC_CaptureCallback()toHAL_TIM_InputCaptureCallback().
Related examples in HAL2 ¶
For more details, refer to the complete example under the firmware directory.
|
Example name |
Brief |
|---|---|
|
input_capture |
How to configure the TIM peripheral in input capture mode to measure the frequency of an external signal. |
Use case: Encoder interface in interrupt mode ¶
|
HAL1 |
HAL2 |
|---|---|
|
Initialization and configuration
Start encoder interface in interrupt mode
TIMx IRQ handler
Callback function
|
Initialization and configuration
Start encoder interface in interrupt mode
TIMx IRQ handler
Callback function
|
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
/* Declaration of tim handle */
TIM_HandleTypeDef htim3;
/* Ensure that all required peripherals
(TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
before starting encoder mode.*/
.............
/* Start the encoder mode in interrupt */
HAL_TIM_Encoder_Start_IT(&htim3, TIM_CHANNEL_ALL);
/* User must call the Interrupt Handler
functions in TIM3 global interrupt */
void TIM3_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim3);
}
/* Call the callback function */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
/* Get the Input Capture value */
uwIC2Value = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_2);
}
|
/* Declaration of the tim handle */
hal_tim_handle_t htim3;
/* Ensure that all required peripherals
(TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
before starting encoder mode.*/
.............
/* Start the encoder mode in interrupt */
HAL_TIM_IC_StartChannel_IT(&htim3, HAL_TIM_CHANNEL_1);
HAL_TIM_IC_StartChannel_IT(&htim3, HAL_TIM_CHANNEL_2);
HAL_TIM_Start(&htim3);
/* User must call the Interrupt Handler
functions in TIM2 global interrupt */
void TIM3_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim3);
}
/* Call the callback function */
void HAL_TIM_InputCaptureCallback(hal_tim_handle_t *htim,
hal_tim_channel_t channel);
{
/* Get the Input Capture value */
uwIC2Value = HAL_TIM_IC_ReadChannelCapturedValue(&htim3, HAL_TIM_CHANNEL_2)
}
|
Key changes ¶
Encoder initialization is unified in HAL2 using
HAL_TIM_Init()+HAL_TIM_SetConfig()and channel configuration uses input-capture configuration APIs.Encoder start in interrupt mode is performed with per-channel start APIs and requires
HAL_TIM_Start().
Use case: External trigger synchro ¶
This example shows how to synchronize TIM peripherals in cascade mode with an external trigger. In this example three timers are used:
TIM1 is slave for an external trigger and master for TIM2.
TIM2 is slave for TIM1.
|
HAL1 |
HAL2 |
|---|---|
|
Initialize and configure timers
Start timers
|
Initialize and configure timers
Start timers
|
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
/* Declaration of tim handle */
TIM_HandleTypeDef htim1, htim2;
TIM_SlaveConfigTypeDef sSlaveConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* Ensure that all required peripherals
(TIM(basic configurations),
GPIO etc.) are configured properly
before starting the timers using STM32CubeMX2 */
.............
/* Configure the slave mode for TIM1*/
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED;
sSlaveConfig.InputTrigger = TIM_TS_TI2FP2;
sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING;
sSlaveConfig.TriggerFilter = 0;
HAL_TIM_SlaveConfigSynchro(&htim1, &sSlaveConfig);
/* Configure the master mode for TIM1*/
sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_ENABLE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
/* Configure the output channel generated by TIM1 to trigger TIM2*/
sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1);
/* Configure the slave mode for TIM2*/
SlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED;
SlaveConfig.InputTrigger = TIM_TS_ITR2;
HAL_TIM_SlaveConfigSynchro(&htim2, &sSlaveConfig)
/* Configure the master mode for TIM2*/
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
/* Configure the output channel generated by TIM2*/
sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
/* Start the timers */
HAL_TIM_IC_Start(&htim1, TIM_CHANNEL_2);
HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_1);
HAL_TIM_OC_Start(&htim2, TIM_CHANNEL_1);
|
/* Declaration of the tim handle */
hal_tim_handle_t htim, htim2;
hal_tim_config_t tim_config;
hal_tim_oc_compare_unit_config_t tim_oc_compare_unit_config;
hal_tim_oc_channel_config_t tim_oc_channel_config;
hal_tim_slave_config_t slave_config;
hal_tim_ic_capture_unit_config_t ic_compare_unit_config;
hal_tim_ic_channel_config_t ic_config;
/* Ensure that all required peripherals
(TIM(basic configurations),
GPIO etc.) are configured properly
before starting the timers using STM32CubeMX2 */
.............
.............
/* Configure the CH2 used to capture the external trigger*/
ic_config.source = HAL_TIM_INPUT_TIM1_TI2_GPIO;
ic_config.polarity = HAL_TIM_IC_RISING;
ic_config.filter = HAL_TIM_FDIV1;
HAL_TIM_IC_SetConfigChannel(&htim1, HAL_TIM_CHANNEL_2, &ic_config);
ic_compare_unit_config.source = HAL_TIM_IC_DIRECT;
ic_compare_unit_config.prescaler = HAL_TIM_IC_DIV1;
HAL_TIM_IC_SetConfigCaptureUnit(&htim1, HAL_TIM_IC_CAPTURE_UNIT_2,
&ic_compare_unit_config);
/* Configure the slave mode for TIM1*/
slave_config.mode = HAL_TIM_SLAVE_GATED;
slave_config.trigger = HAL_TIM_TRIG_TI2FP2;
HAL_TIM_SetSynchroSlave(&htim1, &slave_config);
/* Configure Master mode */
HAL_TIM_SetTriggerOutput(&htim1, HAL_TIM_TRGO_ENABLE);
HAL_TIM_EnableMasterSlaveMode(&htim1);
/* Configure the output channel generated by TIM1 to trigger TIM2*/
tim_oc_channel_config.polarity = HAL_TIM_OC_LOW;
tim_oc_channel_config.idle_state = HAL_TIM_OC_IDLE_STATE_RESET;
HAL_TIM_OC_SetConfigChannel(&htim1, HAL_TIM_CHANNEL_1,
&tim_oc_channel_config);
tim_oc_compare_unit_config.mode = HAL_TIM_OC_PWM1;
tim_oc_compare_unit_config.pulse = PULSE_VALUE;
HAL_TIM_OC_SetConfigCompareUnit(&htim1, HAL_TIM_OC_COMPARE_UNIT_1,
&tim_oc_compare_unit_config);
/* Configure the slave mode for TIM2*/
slave_config.mode = HAL_TIM_SLAVE_GATED;
slave_config.trigger = HAL_TIM_TRIG_ITR2;
HAL_TIM_SetSynchroSlave(&htim2, &slave_config);
/* Configure the output channel generated by TIM2*/
tim_oc_channel_config.polarity = HAL_TIM_OC_LOW;
tim_oc_channel_config.idle_state = HAL_TIM_OC_IDLE_STATE_RESET;
HAL_TIM_OC_SetConfigChannel(&htim2, HAL_TIM_CHANNEL_1,
&tim_oc_channel_config);
tim_oc_compare_unit_config.mode = HAL_TIM_OC_PWM1;
tim_oc_compare_unit_config.pulse = PULSE_VALUE;
HAL_TIM_OC_SetConfigCompareUnit(&htim2, HAL_TIM_OC_COMPARE_UNIT_1,
&tim_oc_compare_unit_config);
/* Start the timers */
HAL_TIM_IC_StartChannel(&htim1, HAL_TIM_CHANNEL_2);
HAL_TIM_OC_StartChannel(&htim1, HAL_TIM_CHANNEL_1);
HAL_TIM_OC_StartChannel(&htim2, HAL_TIM_CHANNEL_1);
HAL_TIM_Start(&htim1);
HAL_TIM_Start(&htim2);
|
Key changes ¶
HAL2 uses dedicated configuration APIs for slave synchronization, master mode, and output channel configuration.
Timer start in HAL2 uses per-channel start APIs and requires explicit timer start using
HAL_TIM_Start().
![@startuml
' To add a number by line
'autonumber
title "Timer as basic counter HAL 1"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as HAL_TIM
participant "MSP" as MSP
participant "IT" as IT
group Initialization
App -[hidden]>HAL_TIM
end
group Start TIMx in IT mode
App -> HAL_TIM: <b><font color=blue>HAL_TIM_Base_Start_IT</b>()
HAL_TIM -> HAL_TIM: Enable UPDATE interrupt\nEnable TIMx counter
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> HAL_TIM: TIMx IRQ
HAL_TIM -> HAL_TIM: HAL_TIM_IRQHandler()
HAL_TIM -> HAL_TIM: Clear flag
HAL_TIM -> App: <b><font color=green>HAL_TIM_PeriodElapsedCallback</b>()
end
@enduml](../../../../../_images/plantuml-f3fabf152c0c9c7a4ae64cb909fdf329df22df44.png)
![@startuml
' To add a number by line
'autonumber
title "Timer as basic counter HAL 2"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as Driver
participant "IT" as IT
group Initialization and Configuration
App -[hidden]>Driver
end
group Start TIMx in IT mode
App->Driver : <b><font color=blue>HAL_TIM_Start_IT</b>()
Driver->Driver : Enable UPDATE interrupt\nEnable TIMx counter
Driver --> App : HAL_OK
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> Driver : HAL_TIM_IRQHandler()
Driver -> Driver : Clear flag
Driver -> App: <b><font color=green>HAL_TIM_UpdateCallback</b>()
App --> Driver
Driver --> IT
end
@enduml](../../../../../_images/plantuml-1db324bb78b314b18831d11c2c8620b9a41695f2.png)
![@startuml
skinparam maxMessageSize 440
' To add a number by line
'autonumber
title "PWM Output in HAL 1"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as Driver
participant "IT" as IT
group Initialization and configuration of TIMx
App -[hidden]>Driver
end
group Configuration of channel
App -> Driver : \n<b><font color=purple>HAL_TIM_PWM_ConfigChannel</b>(channel = <font color=blue>HAL_TIM_CHANNEL_<b>x</b>,\n(TIM_OC_InitTypeDef) {\n.OCPolarity = TIM_OCPOLARITY_HIGH,\n.OCIdleState = TIM_OCIDLESTATE_RESET,\n.OCMode = TIM_OCMODE_PWM1,\n.Pulse = PULSE1_VALUE})
Driver --> App : HAL_O
end
group Start TIMx in IT mode
App -> Driver : <b><font color=blue>HAL_TIM_PWM_Start_IT</b>(&htim, HAL_TIM_CHANNEL_<b>x</b>)
Driver --> App : HAL_OK
Driver->Driver : Enable capture compare interrupt\nEnable TIMx counter
Driver --> App : HAL_OK
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> Driver : HAL_TIM_IRQHandler()
Driver -> Driver : Clear flag
Driver -> App: <b><font color=green>HAL_TIM_PWM_PulseFinishedCallback</b>()
App --> Driver
Driver --> IT
end
@enduml](../../../../../_images/plantuml-21ba407302b7a55a985ced43ca06a3094035a53e.png)
![@startuml
skinparam maxMessageSize 440
' To add a number by line
'autonumber
title "PWM Output in HAL 2"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as Driver
participant "IT" as IT
group Initialization and configuration of TIMx
App -[hidden]>Driver
end
group Configuration of channel
App -> Driver : \n<b><font color=purple>HAL_TIM_OC_SetConfigChannel</b>(channel = <font color=blue>HAL_TIM_CHANNEL_<b>x</b>,\n(hal_tim_oc_channel_config_t) {\n.polarity = HAL_TIM_OC_HIGH,\n.idle_state = HAL_TIM_OC_IDLE_STATE_RESET})
Driver --> App : HAL_OK
App -> Driver : \n<b><font color=purple>HAL_TIM_OC_SetConfigCompareUnit</b>(\ncompare_unit = <font color=blue>HAL_TIM_OC_COMPARE_UNIT_<b>x</b>,\n(hal_tim_oc_compare_unit_config_t ) {\n.mode = <font color=black>HAL_TIM_OC_PWM1,\n.pulse = PULSE1_VALUE})
Driver --> App : HAL_OK
end
group Start TIMx in IT mode
App -> Driver : <b><font color=blue>HAL_TIM_OC_StartChannel_IT</b>(&htimx, HAL_TIM_CHANNEL_<b>x</b>)
Driver --> App : HAL_OK
Driver->Driver : Enable the channel\nEnable capture compare interrupt.
Driver --> App : HAL_OK
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> Driver : HAL_TIM_CC_IRQHandler()
Driver -> Driver : Clear flag
App -> Driver : <b><font color=blue>HAL_TIM_Start</b>()
Driver->Driver : Enable TIMx counter
Driver --> App : HAL_OK
Driver -> App: <b><font color=green>HAL_TIM_CompareMatchCallback</b>()
end
@enduml](../../../../../_images/plantuml-641dd49b5aae60ea51b054279c3300b48f1e4a6c.png)
![@startuml
skinparam maxMessageSize 440
' To add a number by line
'autonumber
title "Input Capture in HAL 1"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as Driver
participant "IT" as IT
group Initialization and configuration of TIMx
App -[hidden]>Driver
end
group Configuration of channel
App -> Driver : \n<b><font color=blue>HAL_TIM_IC_ConfigChannel</b>(channel = <font color=blue>HAL_TIM_CHANNEL_<b>x</b>,\n(TIM_IC_InitTypeDef) {\n.ICPolarity = TIM_ICPOLARITY_RISING,\n.ICSelection = TIM_ICSELECTION_DIRECTTI,\n.ICPrescaler = TIM_ICPSC_DIV1,\n.ICFilter = 0})
Driver --> App : HAL_OK
end
group Start TIMx in IT mode
App -> Driver : HAL_TIM_IC_Start_IT(&htim, HAL_TIM_CHANNEL_<b>x</b>)
Driver --> App : HAL_OK
Driver->Driver : Enable capture compare interrupt\nEnable TIMx counter
Driver --> App : HAL_OK
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> Driver : HAL_TIM_IRQHandler()
Driver -> Driver : Clear flag
Driver -> App: HAL_TIM_IC_CaptureCallback()
App --> Driver
Driver --> IT
end
@enduml](../../../../../_images/plantuml-27c4d2aa38ba6a63f999e602692ef56f23568147.png)
![@startuml
skinparam maxMessageSize 440
' To add a number by line
'autonumber
title "Input Capture in HAL 2"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as Driver
participant "IT" as IT
group Initialization and configuration of TIMx
App -[hidden]>Driver
end
group Configuration of channel
App -> Driver : \n<b><font color=blue>HAL_TIM_IC_SetConfigChannel</b>(channel = <font color=blue>HAL_TIM_CHANNEL_<b>x</b>,\n(hal_tim_ic_channel_config_t) {\n.polarity = HAL_TIM_IC_RISING,\n.selection = HAL_TIM_IC_DIRECTTI,\n.prescaler = HAL_TIM_IC_DIV1,\n.filter = 0})
Driver --> App : HAL_OK
App -> Driver : \n<b><font color=blue>HAL_TIM_IC_SetConfigCaptureUnit</b>(\ncompare_unit = <font color=blue>HAL_TIM_IC_CAPTURE_UNIT_<b>x</b>,\n(hal_tim_ic_capture_unit_config_t ) {\n.mode = <font color=black>HAL_TIM_IC_PWM1})
Driver --> App : HAL_OK
end
group Start TIMx in IT mode
App -> Driver : HAL_TIM_IC_StartChannel_IT(HAL_TIM_CHANNEL_<b>x</b>)
Driver --> App : HAL_OK
Driver->Driver : Enable the channel\nEnable capture compare interrupt.
Driver --> App : HAL_OK
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> Driver : HAL_TIM_IRQHandler()
Driver -> Driver : Clear flag
App -> Driver : HAL_TIM_Start()
Driver->Driver : Enable TIMx counter
Driver --> App : HAL_OK
Driver -> App: HAL_TIM_InputCaptureCallback()
end
@enduml](../../../../../_images/plantuml-f9c40aef6ade598837fd2b3ff3f099ab5107b942.png)
![@startuml
title "Encoder Mode in HAL 1"
participant "User Application" as App
participant "HAL TIM" as Driver
participant "MSP" as MSP
group Initialization and Configuration
App -> Driver: <b><font color=purple>HAL_TIM_Encoder_Init</b>(&(TIM_Encoder_InitTypeDef){\nSet Instance (TIMx)\nSet Init structure\nEncoderMode = TIM_ENCODERMODE_TI12,\nIC1Polarity = TIM_ICPOLARITY_RISING,\nIC1Selection = TIM_ICSELECTION_DIRECTTI,\nIC1Prescaler = TIM_ICPSC_DIV1,\nIC1Filter = 0,\nIC2Polarity = TIM_ICPOLARITY_RISING,\nIC2Selection = TIM_ICSELECTION_DIRECTTI,\nIC2Prescaler = TIM_ICPSC_DIV1,\nIC2Filter = 0})
Driver -> MSP: HAL_TIM_ENCODER_MspInit()
MSP -> Driver: Enable TIMx clock\nCH1, CH2 GPIO AF config
end
group Start TIMx in IT Mode
App -> Driver: <b><font color=green>HAL_TIM_Encoder_Start_IT</b>(TIM_CHANNEL_ALL)
Driver->Driver : Enable capture compare interrupt\nEnable TIMx counter
Driver --> App : HAL_OK
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> Driver : HAL_TIM_IRQHandler()
Driver -> Driver : Clear flag
Driver -> App: HAL_TIM_IC_CaptureCallback()
App --> Driver
Driver --> IT
end
@enduml](../../../../../_images/plantuml-70c4e259869952a4fe615bf469337dcf2098dc47.png)
![@startuml
skinparam maxMessageSize 440
' To add a number by line
'autonumber
title "Encoder Mode in HAL 2"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as Driver
group Initialization
App -> Driver : <b><font color=purple>HAL_TIM_Init()</b>
Driver -> MSP: TIM_MspInit()
MSP -> Driver: Enable TIMx clock\nEnable TIMx interrupt
Driver --> App : HAL_OK
end
group Configuration
App->Driver : <b><font color=purple>HAL_TIM_SetConfig</b>((hal_tim_config_t) {\n.prescaler = α\n.counter_mode = HAL_TIM_COUNTER_UP,\n.period = β,\n.clock_source = HAL_TIM_CLK_ENCODER_X4_TI12}
Driver -> Driver : Configure TIMx time base
Driver --> App : HAL_OK
App -> Driver : <b><font color=purple>HAL_TIM_IC_SetConfigChannel</b>(channel = HAL_TIM_CHANNEL_1,\nhal_tim_ic_channel_config_t {\nsource = HAL_TIMx_TI1_GPIO,\npolarity = HAL_TIM_IC_POLARITY_RISING,\nfilter = HAL_TIM_FDIV1})
Driver --> App : HAL_OK
App -> Driver : <b><font color=purple>HAL_TIM_IC_SetConfigCaptureUnit</b>(capture_unit = HAL_TIM_CAPTURE_UNIT_1,\nhal_tim_ic_capture_config_t {\nactive_input = HAL_TIM_IC_DIRECT,\nprescaler = HAL_TIM_IC_DIV1})
Driver --> App : HAL_OK
App -> Driver : <b><font color=purple>HAL_TIM_IC_SetConfigChannel</b>(channel = HAL_TIM_CHANNEL_2,\nhal_tim_ic_channel_config_t {\nsource = HAL_TIMx_TI2_GPIO,\npolarity = HAL_TIM_IC_POLARITY_RISING,\nfilter = HAL_TIM_FDIV1})
Driver --> App : HAL_OK
App -> Driver : <b><font color=purple>HAL_TIM_IC_SetConfigCaptureUnit</b>(capture_unit = HAL_TIM_CAPTURE_UNIT_2,\nhal_tim_ic_capture_config_t {\nactive_input = HAL_TIM_IC_DIRECT,\nprescaler = HAL_TIM_IC_DIV1})
Driver --> App : HAL_OK
end
group Start encoder interface in IT mode
App -> Driver : <b><font color=green>HAL_TIM_IC_StartChannel_IT</b>(HAL_TIM_CHANNEL_1)
Driver --> App : HAL_OK
App -> Driver : <b><font color=green>HAL_TIM_IC_StartChannel_IT</b>(HAL_TIM_CHANNEL_2)
Driver --> App : HAL_OK
Driver->Driver : Enable the channel\nEnable capture compare interrupt.
Driver --> App : HAL_OK
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> Driver : HAL_TIM_IRQHandler()
Driver -> Driver : Clear flag
App -> Driver : <b><font color=green>HAL_TIM_Start</b>()
Driver->Driver : Enable TIMx counter
Driver --> App : HAL_OK
Driver -> App: HAL_TIM_InputCaptureCallback()
end
@enduml](../../../../../_images/plantuml-61b0e989066935bdc4b1b4115c214d6e9c37b90c.png)
![@startuml
skinparam maxMessageSize 450
' To add a number by line
'autonumber
title "ExtTriggerSynchro in Cube 2"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as Driver
group Initialization
App -[hidden]>Driver
App -> Driver : Configure TIM1, TIM2 time base
Driver -> App : HAL_OK
end
group Configuration of master and salve timer
App -> Driver : Configure TIM1 as slave for external trigger and master for TIM2.
Driver --> App : HAL_OK
App -> Driver : Configure TIM2 as slave for TIM1.
Driver --> App : HAL_OK
App -> App : CH1, CH2 GPIO AF config
end
group Start TIM1 and TIM2
App -> Driver : <b>HAL_TIM_IC_Start</b>(&htim<b><font color=red>1</b>, TIM_CHANNEL_2)
App -> Driver : <b>HAL_TIM_OC_Start</b>(&htim<b><font color=red>1</b>, TIM_CHANNEL_1)
Driver --> App : HAL_OK
App -> Driver : <b>HAL_TIM_OC_Start</b>(&htim<b><font color=green>2</b>, TIM_CHANNEL_1)
Driver --> App : HAL_OK
end
@enduml](../../../../../_images/plantuml-f617acc0f4b9b2b1d348d911eafdea9b15360f1b.png)
![@startuml
skinparam maxMessageSize 450
' To add a number by line
'autonumber
title "ExtTriggerSynchro in Cube 2"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as Driver
group Initialization
App -[hidden]>Driver
App -> Driver : Configure TIM1, TIM2 time base
Driver -> App : HAL_OK
end
group Configuration of master and salve timer
App -> Driver : Configure TIM1 as slave for external trigger and master for TIM2.
Driver --> App : HAL_OK
App -> Driver : Configure TIM2 as slave for TIM1.
Driver --> App : HAL_OK
App -> App : CH1, CH2 GPIO AF config
end
group Start TIM1 and TIM2
App -> Driver : <b>HAL_TIM_IC_StartChannel</b>(&htim<b><font color=red>1</b>, HAL_TIM_CHANNEL_2)
App -> Driver : <b>HAL_TIM_OC_StartChannel</b>(&htim<b><font color=red>1</b>, HAL_TIM_CHANNEL_1)
App -> Driver : <b>HAL_TIM_Start</b>(&htim<b><font color=red>1</b>)
Driver --> App : HAL_OK
App -> Driver : <b>HAL_TIM_OC_StartChannel</b>(&htim<b><font color=green>2</b>, HAL_TIM_CHANNEL_1)
App -> Driver : <b>HAL_TIM_Start</b>(&htim<b><font color=green>2</b>)
Driver --> App : HAL_OK
end
@enduml](../../../../../_images/plantuml-43123a0191f81b5b525c211075440d320f904bf4.png)